import requests
import pprint
from pymongo import MongoClient

#将数据保存到Mongodb中
client=MongoClient()
#创建数据库
database=client['weibo--user']
#创建集合
db=database['user']
header={
'User-Agent':'Chrome/98.0.4758.102 Mobile Safari/537.36',
'content-type':'application/json; charset=utf-8'
}
#爬取作者个人微博

all_data=[]
# page=input('请输入页码:')
for page in range(1,10):
    url='https://m.weibo.cn/api/container/getIndex?jumpfrom=weibocom&type=uid&value=1768167034&containerid=1076031768167034&page={}'.format(page)
    response=requests.get(url,headers=header)
    #获取json格式数据
    data=response.json()
    cars=data['data']['cards']
    # pprint.pprint(cars)
    for i in cars:
        mlog=i.get('mblog',None)
        if mlog:
              df={}
    #         有内容才提取
              df['user_name'] = mlog.get('user', {}).get('screen_name', None)
              df['source'] = mlog.get('source', None)
              df['text']=mlog.get('text',None)
              all_data.append(df)

#插入数据
db.insert_many(all_data)

#验证
# content=[x for x in db.find()]
# print(len(content))